package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.MyReleaseGoodsAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.GoodsDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.view.PromptPopupWindow;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyReleaseGoodsActivity extends BaseActivity {


    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;


    PromptPopupWindow promptPopupWindow;

    int position = 0;
    String goodsId = "";
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_release_goods, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("我发布的闲置物品");


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        myReleaseGoodsAdapter = new MyReleaseGoodsAdapter(this, new ArrayList<GoodsDataBean>());
        myReleaseGoodsAdapter.setOnClickCallBack(new MyReleaseGoodsAdapter.OnClickCallBack() {
            @Override
            public void deleteGoods(int p, GoodsDataBean item) {

                position = p;
                goodsId = item.getId();

                if (promptPopupWindow == null) {
                    promptPopupWindow = new PromptPopupWindow(MyReleaseGoodsActivity.this, toolbar);
                    promptPopupWindow.setCallBack(new PromptPopupWindow.CallBack() {
                        @Override
                        public void confirm() {
                            deleteGd();
//                            delForum();
                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }

                promptPopupWindow.showPopupWindow();

            }


            @Override
            public void editGoods(GoodsDataBean item) {
                underGoods(item);
            }
        });
        recycleView.setAdapter(myReleaseGoodsAdapter);

        getDatas();
    }

    MyReleaseGoodsAdapter myReleaseGoodsAdapter;

    private void getDatas() {
        Map map = new HashMap();

        map.put("token", SPUtils.getToken());
        map.put("userId", SPUtils.getUserID());
        HttpMehtod.getInstance().myUserGoods(map, new IdeaObserver<BaseDataBean<List<GoodsDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<GoodsDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    promptImg.setVisibility(View.GONE);
                    myReleaseGoodsAdapter.notifyDataSetChanged((ArrayList<GoodsDataBean>) listBaseDataBean.getData());
                } else {
                    myReleaseGoodsAdapter.notifyDataSetChanged(new ArrayList<GoodsDataBean>());
                    promptImg.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFail(int errorCode) {
                myReleaseGoodsAdapter.notifyDataSetChanged(new ArrayList<GoodsDataBean>());
            }
        });
    }

    private void underGoods(final GoodsDataBean item) {
        Map map = new HashMap();
        map.put("goodsId", item.getId());

        if (item.getIs_publish().equals("0")) {
            map.put("isPublish", "1");
        } else {
            map.put("isPublish", "0");
        }

        HttpMehtod.getInstance().underGoods(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("成功");


                if (item.getIs_publish().equals("0")) {
                    item.setIs_publish("1");


                    ToastUtil.show("上架成功");
                } else if (item.getIs_publish().equals("1")) {
                    item.setIs_publish("0");

                    ToastUtil.show("下架成功");
                }
                myReleaseGoodsAdapter.notifyDataSetChanged();
//                getDatas();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }


    private void deleteGd() {
        Map map = new HashMap();
        map.put("goodsId", goodsId);
        HttpMehtod.getInstance().deleteGoods(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("删除成功");
                myReleaseGoodsAdapter.itemDatas.remove(position);
                myReleaseGoodsAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

}
